package com.blackhorse.anlis.anli_tx.serviceImpl;

import com.blackhorse.dao.AccountDao;
import com.blackhorse.domain.Account;
import com.blackhorse.service.AccountService;
import com.blackhorse.utils.TransactionManager;

import java.util.List;

/**
 * 具有自定义事务管理功能的service实现类
 * 存在的问题：代码复杂，没有做到重用性
 */
public class AccountServiceTransactionImpl implements AccountService {

    private AccountDao accountDao;
    private TransactionManager transactionManager;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void setTransactionManager(TransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    @Override
    public List<Account> findAllAccount() {
        try {
            //1.开启事务
            transactionManager.beginTransaction();
            //2.执行操作
            List<Account> accounts = accountDao.findAllAccount();
            //3.提交事务
            transactionManager.commit();
            //4.返回结果
            return accounts;
        } catch (Exception e) {
            e.printStackTrace();
            //5.事务回滚
            transactionManager.rollback();
            return null;
        } finally {
            //6.释放连接
            transactionManager.release();
        }
    }

    @Override
    public Account findAccountById(int id) {
        try {
            //1.开启事务
            transactionManager.beginTransaction();
            //2.执行操作
            Account account = accountDao.findAccountById(id);
            //3.提交事务
            transactionManager.commit();
            //4.返回结果
            return account;
        } catch (Exception e) {
            e.printStackTrace();
            //5.事务回滚
            transactionManager.rollback();
            return null;
        } finally {
            //6.释放连接
            transactionManager.release();
        }
    }

    @Override
    public void saveAccount(Account account) {
        try {
            //1.开启事务
            transactionManager.beginTransaction();
            //2.执行操作
            accountDao.saveAccount(account);
            //3.提交事务
            transactionManager.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //5.事务回滚
            transactionManager.rollback();
        } finally {
            //6.释放连接
            transactionManager.release();
        }
    }

    @Override
    public void updateAccount(Account account) {
        try {
            //1.开启事务
            transactionManager.beginTransaction();
            //2.执行操作
            accountDao.updateAccount(account);
            //3.提交事务
            transactionManager.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //5.事务回滚
            transactionManager.rollback();
        } finally {
            //6.释放连接
            transactionManager.release();
        }

    }

    @Override
    public void deleteAccount(int id) {
        try {
            //1.开启事务
            transactionManager.beginTransaction();
            //2.执行操作
            accountDao.deleteAccount(id);
            //3.提交事务
            transactionManager.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //5.事务回滚
            transactionManager.rollback();
        } finally {
            //6.释放连接
            transactionManager.release();
        }

    }

    /**
     * 转账操作
     *
     * @param srcName 转出账户名
     * @param tarName 转入账户名
     * @param money   转出钱数
     */
    @Override
    public void transter(String srcName, String tarName, float money) {
        try {
            //1.开启事务
            transactionManager.beginTransaction();
            //2.执行操作
            Account srcAccount = accountDao.findAccountByName(srcName);
            Account tarAccount = accountDao.findAccountByName(tarName);

            srcAccount.setMoney(srcAccount.getMoney() - money);
            tarAccount.setMoney(tarAccount.getMoney() + money);

            accountDao.updateAccount(srcAccount);
            //如果这里出现异常，事务回滚
            int a = 1 / 0;
            accountDao.updateAccount(tarAccount);

            //3.提交事务
            transactionManager.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //5.事务回滚
            transactionManager.rollback();
        } finally {
            //6.释放连接
            transactionManager.release();
        }

    }

}
